<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>StringBuilder 类详解 - Java 字符串处理利器</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .noto-serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .transition-all {
            transition: all 0.3s ease;
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(167, 139, 250, 0.3);
            z-index: -1;
            transform: scaleX(0);
            transform-origin: left;
            transition: transform 0.3s ease;
        }
        .highlight:hover:after {
            transform: scaleX(1);
        }
        .first-letter::first-letter {
            font-size: 3.5rem;
            font-weight: bold;
            float: left;
            line-height: 1;
            margin-right: 0.5rem;
            color: #3b82f6;
        }
        .code-block {
            background-color: #1e293b;
            border-left: 4px solid #8b5cf6;
        }
    </style>
</head>
<body class="min-h-screen">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-28">
        <div class="max-w-4xl mx-auto">
            <div class="flex items-center mb-6">
                <span class="bg-white text-indigo-600 px-3 py-1 rounded-full text-sm font-medium">Java 编程</span>
                <span class="ml-2 text-indigo-100 text-sm"><i class="fas fa-clock mr-1"></i> 阅读时间: 5 分钟</span>
            </div>
            <h1 class="noto-serif text-4xl md:text-5xl font-bold leading-tight mb-6">
                <span class="highlight">StringBuilder</span> 类 - Java 字符串处理的<span class="highlight">高效利器</span>
            </h1>
            <p class="text-xl text-indigo-100 max-w-3xl leading-relaxed mb-8">
                探索 Java 中高效处理可变字符串的核心工具，理解其设计原理、使用场景以及与 StringBuffer 的关键区别。
            </p>
            <div class="flex items-center">
                <div class="w-10 h-10 rounded-full bg-indigo-500 flex items-center justify-center mr-3">
                    <i class="fas fa-user text-white"></i>
                </div>
                <div>
                    <p class="font-medium">Java 技术专家</p>
                    <p class="text-indigo-100 text-sm">最后更新: 2023年11月</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-4xl mx-auto px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="first-letter text-lg leading-relaxed text-slate-700 mb-8">
                字符串是一种常见的数据类型，而 `StringBuilder` 类则是用于处理可变字符串的重要工具之一。与 `StringBuffer` 类类似，`StringBuilder` 类也提供了一系列便捷的操作方法，但它不是线程安全的。
            </p>
            
            <!-- Diagram -->
            <div class="bg-white rounded-xl shadow-md p-6 mb-12 transition-all hover:shadow-lg">
                <div class="mermaid">
                    graph TD
                        A[字符串类型] --> B(String)
                        A --> C(StringBuffer)
                        A --> D(StringBuilder)
                        C -->|线程安全| E[多线程环境]
                        D -->|非线程安全| F[单线程环境]
                        B -->|不可变| G[每次修改创建新对象]
                        C & D -->|可变| H[直接修改原对象]
                </div>
                <p class="text-sm text-slate-500 mt-4 text-center">
                    ▲ 三种字符串类型的关系与特点对比
                </p>
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-lg bg-blue-100 text-blue-600 flex items-center justify-center mr-4">
                    <i class="fas fa-info-circle text-xl"></i>
                </div>
                <h2 class="noto-serif text-2xl md:text-3xl font-bold text-slate-800">1. StringBuilder 类概述</h2>
            </div>
            <div class="bg-white rounded-xl shadow-sm p-6 md:p-8 transition-all card-hover">
                <p class="text-slate-700 leading-relaxed mb-4">
                    `StringBuilder` 类是 `java.lang` 包中的一个类，用于表示可变的字符序列。与 `String` 类和 `StringBuffer` 类不同，`StringBuilder` 类的对象是可变的，可以动态修改其内容，而不会创建新的对象。
                </p>
                <div class="flex flex-wrap gap-4 mt-6">
                    <div class="flex-1 min-w-[200px] bg-blue-50 p-4 rounded-lg">
                        <div class="text-blue-600 mb-2"><i class="fas fa-cube mr-2"></i>包位置</div>
                        <p class="text-sm text-slate-700">java.lang.StringBuilder</p>
                    </div>
                    <div class="flex-1 min-w-[200px] bg-blue-50 p-4 rounded-lg">
                        <div class="text-blue-600 mb-2"><i class="fas fa-shield-alt mr-2"></i>线程安全</div>
                        <p class="text-sm text-slate-700">非线程安全</p>
                    </div>
                    <div class="flex-1 min-w-[200px] bg-blue-50 p-4 rounded-lg">
                        <div class="text-blue-600 mb-2"><i class="fas fa-tachometer-alt mr-2"></i>性能</div>
                        <p class="text-sm text-slate-700">单线程下性能最优</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-lg bg-purple-100 text-purple-600 flex items-center justify-center mr-4">
                    <i class="fas fa-star text-xl"></i>
                </div>
                <h2 class="noto-serif text-2xl md:text-3xl font-bold text-slate-800">2. StringBuilder 类的特点</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-sm p-6 transition-all card-hover">
                    <div class="text-purple-600 text-2xl mb-4"><i class="fas fa-exchange-alt"></i></div>
                    <h3 class="font-bold text-lg text-slate-800 mb-3">可变性</h3>
                    <p class="text-slate-600 text-sm leading-relaxed">
                        `StringBuilder` 类的对象是可变的，可以通过一系列方法修改其内容，而不会创建新的对象。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-sm p-6 transition-all card-hover">
                    <div class="text-purple-600 text-2xl mb-4"><i class="fas fa-user-lock"></i></div>
                    <h3 class="font-bold text-lg text-slate-800 mb-3">非线程安全</h3>
                    <p class="text-slate-600 text-sm leading-relaxed">
                        与 `StringBuffer` 类不同，`StringBuilder` 类不是线程安全的，适用于单线程环境下的字符串操作。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-sm p-6 transition-all card-hover">
                    <div class="text-purple-600 text-2xl mb-4"><i class="fas fa-expand"></i></div>
                    <h3 class="font-bold text-lg text-slate-800 mb-3">容量自动调整</h3>
                    <p class="text-slate-600 text-sm leading-relaxed">
                        `StringBuilder` 类会自动调整其内部的字符数组容量，以适应字符串的长度变化。当字符串长度超过当前容量时，会自动扩容。
                    </p>
                </div>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-lg bg-green-100 text-green-600 flex items-center justify-center mr-4">
                    <i class="fas fa-list-ul text-xl"></i>
                </div>
                <h2 class="noto-serif text-2xl md:text-3xl font-bold text-slate-800">3. StringBuilder 类的常用操作方法</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">方法</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">描述</th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">示例</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr class="hover:bg-gray-50 transition-all">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-blue-600">append()</td>
                            <td class="px-6 py-4 text-sm text-gray-700">将指定内容追加到字符串的末尾</td>
                            <td class="px-6 py-4 text-sm text-gray-500 font-mono">sb.append(" World")</td>
                        </tr>
                        <tr class="hover:bg-gray-50 transition-all">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-blue-600">insert()</td>
                            <td class="px-6 py-4 text-sm text-gray-700">在指定位置插入指定内容</td>
                            <td class="px-6 py-4 text-sm text-gray-500 font-mono">sb.insert(5, ", ")</td>
                        </tr>
                        <tr class="hover:bg-gray-50 transition-all">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-blue-600">delete()</td>
                            <td class="px-6 py-4 text-sm text-gray-700">删除指定范围内的字符</td>
                            <td class="px-6 py-4 text-sm text-gray-500 font-mono">sb.delete(5, 7)</td>
                        </tr>
                        <tr class="hover:bg-gray-50 transition-all">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-blue-600">reverse()</td>
                            <td class="px-6 py-4 text-sm text-gray-700">颠倒字符串中字符的顺序</td>
                            <td class="px-6 py-4 text-sm text-gray-500 font-mono">sb.reverse()</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-lg bg-yellow-100 text-yellow-600 flex items-center justify-center mr-4">
                    <i class="fas fa-code text-xl"></i>
                </div>
                <h2 class="noto-serif text-2xl md:text-3xl font-bold text-slate-800">4. 使用示例</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <div class="code-block text-gray-200 p-6 overflow-x-auto">
                    <pre><code class="language-java">public class StringBuilderDemo {
    public static void main(String[] args) {
        // 创建一个 StringBuilder 对象
        StringBuilder stringBuilder = new StringBuilder("Hello");

        // 追加字符串
        stringBuilder.append(" World");
        System.out.println("追加字符串后：" + stringBuilder);

        // 在指定位置插入字符串
        stringBuilder.insert(5, ", ");
        System.out.println("插入字符串后：" + stringBuilder);

        // 删除指定范围内的字符
        stringBuilder.delete(5, 7);
        System.out.println("删除字符串后：" + stringBuilder);

        // 颠倒字符串中字符的顺序
        stringBuilder.reverse();
        System.out.println("颠倒字符串后：" + stringBuilder);
    }
}</code></pre>
                </div>
                <div class="p-6 border-t border-gray-200">
                    <h4 class="text-sm font-medium text-gray-500 mb-2">输出结果：</h4>
                    <div class="bg-gray-50 p-4 rounded-md">
                        <p class="text-sm text-gray-700 font-mono mb-1">追加字符串后：Hello World</p>
                        <p class="text-sm text-gray-700 font-mono mb-1">插入字符串后：Hello,  World</p>
                        <p class="text-sm text-gray-700 font-mono mb-1">删除字符串后：Hello World</p>
                        <p class="text-sm text-gray-700 font-mono">颠倒字符串后：dlroW olleH</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 5 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-lg bg-red-100 text-red-600 flex items-center justify-center mr-4">
                    <i class="fas fa-balance-scale text-xl"></i>
                </div>
                <h2 class="noto-serif text-2xl md:text-3xl font-bold text-slate-800">5. StringBuilder 类与 StringBuffer 类的区别</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-sm p-6 transition-all card-hover">
                <div class="flex flex-col md:flex-row gap-8">
                    <div class="flex-1">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-red-100 text-red-600 flex items-center justify-center mr-3">
                                <i class="fas fa-user-shield"></i>
                            </div>
                            <h3 class="font-bold text-lg">StringBuffer</h3>
                        </div>
                        <ul class="space-y-3 text-slate-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2 flex-shrink-0"></i>
                                <span>线程安全，方法都是同步的</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2 flex-shrink-0"></i>
                                <span>适用于多线程环境</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2 flex-shrink-0"></i>
                                <span>性能相对较低</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="flex-1">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center mr-3">
                                <i class="fas fa-user"></i>
                            </div>
                            <h3 class="font-bold text-lg">StringBuilder</h3>
                        </div>
                        <ul class="space-y-3 text-slate-700">
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-500 mt-1 mr-2 flex-shrink-0"></i>
                                <span>非线程安全，没有同步</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2 flex-shrink-0"></i>
                                <span>适用于单线程环境</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2 flex-shrink-0"></i>
                                <span>性能更高</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 6 -->
        <section>
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-lg bg-orange-100 text-orange-600 flex items-center justify-center mr-4">
                    <i class="fas fa-exclamation-triangle text-xl"></i>
                </div>
                <h2 class="noto-serif text-2xl md:text-3xl font-bold text-slate-800">6. 注意事项</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-sm p-8 transition-all card-hover">
                <div class="flex flex-col md:flex-row gap-6">
                    <div class="flex-1 bg-orange-50 p-6 rounded-lg">
                        <div class="text-orange-600 text-2xl mb-4"><i class="fas fa-users-slash"></i></div>
                        <h3 class="font-bold text-lg text-slate-800 mb-3">线程安全性</h3>
                        <p class="text-slate-600 text-sm leading-relaxed">
                            在单线程环境下使用 `StringBuilder` 类时，应注意线程安全性问题。如果在多线程环境中使用，应考虑使用 `StringBuffer` 或自行处理同步。
                        </p>
                    </div>
                    <div class="flex-1 bg-orange-50 p-6 rounded-lg">
                        <div class="text-orange-600 text-2xl mb-4"><i class="fas fa-tachometer-alt"></i></div>
                        <h3 class="font-bold text-lg text-slate-800 mb-3">性能优化</h3>
                        <p class="text-slate-600 text-sm leading-relaxed">
                            当需要进行大量字符串拼接或修改操作时，建议使用 `StringBuilder` 类以提高性能，避免频繁创建新的 `String` 对象。
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 代码高亮效果
        document.querySelectorAll('code.language-java').forEach(el => {
            const keywords = ['public', 'class', 'static', 'void', 'new', 'System', 'out', 'println'];
            let html = el.innerHTML;
            
            keywords.forEach(keyword => {
                const regex = new RegExp(`\\b${keyword}\\b`, 'g');
                html = html.replace(regex, `<span class="text-purple-400">${keyword}</span>`);
            });
            
            // 高亮字符串
            html = html.replace(/"([^"]*)"/g, '<span class="text-green-400">"$1"</span>');
            
            // 高亮注释
            html = html.replace(/\/\/.*$/gm, '<span class="text-gray-500">$&</span>');
            
            el.innerHTML = html;
        });
    </script>
</body>
</html>